package com.junmo.wechat.controller.app;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.junmo.common.constant.RedisConstants;
import com.junmo.common.enums.RemindCode;
import com.junmo.common.exception.RemindException;
import com.junmo.common.redis.manager.RedisCacheManager;
import com.junmo.common.redis.utils.RedisUtils;
import com.junmo.common.result.Result;
import com.junmo.common.sms.model.SmsSendModel;
import com.junmo.common.sms.properties.DySmsEnum;
import com.junmo.common.sms.service.AliyunSmsService;
import com.junmo.common.util.StringUtils;
import com.junmo.wechat.pojo.vo.MemberVO;
import com.junmo.wechat.service.IMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.bouncycastle.crypto.engines.CAST5Engine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Author: zjj
 * @Date: 2022/7/11
 */
@Api(tags = "「移动端」验证码")
@RestController
@RequestMapping("/app-api/v1/sms")
@RequiredArgsConstructor
public class CaptchaController {

    @Autowired
    private RedisCacheManager redisCacheManager;

    @Autowired
    private IMemberService memberService;

    @Autowired
    private AliyunSmsService aliyunSmsService;

    /**
     * 短信验证码发送接口(注册、登录、忘记密码)
     *
     * @param smsLoginModel
     * @return
     */
    @ApiOperation("短信验证码发送接口")
    @PostMapping(value = "/_send")
    public Result smsSend(@RequestBody SmsSendModel smsLoginModel) {
        String mobile = smsLoginModel.getPhoneNumber();

        if (StringUtils.isEmpty(mobile)) {
            throw new RemindException(RemindCode.PHONE_NULL_REMIND);
        }
        Object object = redisCacheManager.getSmsCaptcha(smsLoginModel.getType().getName(), mobile);
        if (object != null) {
            throw new RemindException(RemindCode.USER_CAPTCHA_IN_USE);
        }

        //随机数验证码
        String smsCode = RandomUtil.randomNumbers(6);
        JSONObject obj = new JSONObject();
        obj.put("code", smsCode);
        try {
            MemberVO memberInfo = memberService.getMemberInfoByMobile(mobile);

            if (DySmsEnum.REGISTER_SMS_TEMPLATE == smsLoginModel.getType() && memberInfo != null) {
                //注册模版
                throw new RemindException(RemindCode.PHONE_USER_EXIST_REMIND);
            } else {
                //校验用户有效性
                memberService.checkUserIsEffective(memberInfo);
            }

            boolean b = aliyunSmsService.sendSmsCode(mobile, obj, DySmsEnum.REGISTER_SMS_TEMPLATE);

            if (b == false) {
                throw new RemindException(RemindCode.USER_CAPTCHA_SEND_ERROR);
            }

            redisCacheManager.setSmsCaptcha(smsLoginModel.getType().getName(),mobile, smsCode);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("验证码发送失败");
        }
        return Result.success();
    }
}
